
package cc.dd.marketing.core;


import cc.dd.marketing.api.constant.FunctionCodeEnum;
import cc.dd.marketing.core.base.BusinessHandler;
import cc.dd.marketing.core.base.RequestParser;
import cc.dd.marketing.core.base.ResponseParser;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dongsufeng
 * @date 2022/9/9 2:31 PM
 */
@Component
public class CommonFactory {

    public static Container container = new Container();

    /**
     * 获取活动处理器
     *
     * @param function
     * @param businessNo
     * @return
     */
    public BusinessHandler getBusinessHandler(FunctionCodeEnum function, String businessNo) {
        BusinessHandler functionHandler = (BusinessHandler) container.get(BusinessHandler.class, function, businessNo);
        if (functionHandler == null) {
            String err = "functionCode :" + function + " - FunctionHandler mismatch";
            throw new IllegalArgumentException(err);
        } else {
            return functionHandler;
        }
    }

    /**
     * 获取活动数据传输对象解析器
     *
     * @param function
     * @param businessNo
     * @return
     */
    public RequestParser getBusinessReqParser(FunctionCodeEnum function, String businessNo) {
        RequestParser activityReqParser = (RequestParser) container.get(RequestParser.class, function, businessNo);
        if (activityReqParser == null) {
            String err = "functionCode :" + function + " - activityDTOParser mismatch";
            throw new IllegalArgumentException(err);
        } else {
            return activityReqParser;
        }
    }

    /**
     * 获取活动响应对象解析器
     *
     * @param function
     * @param activityType
     * @return
     */
    public ResponseParser getResponseParser(FunctionCodeEnum function, String activityType) {
        ResponseParser activityResponseParser = (ResponseParser) container.get(ResponseParser.class, function, activityType);
        if (activityResponseParser == null) {
            String err = "functionCode :" + function + " - activityResponseParser mismatch";
            throw new IllegalArgumentException(err);
        } else {
            return activityResponseParser;
        }
    }

    /**
     * 容器类
     */
    public static class Container {
        public static Map<Class, Map<String, Map<String, Object>>> beanMap;

        public Container() {
            this.beanMap = new HashMap<>();
        }

        /**
         * 往容器中添加对象
         *
         * @param clazz
         * @param function
         * @param businessNo
         * @param bean
         */
        public static void put(Class clazz, FunctionCodeEnum function, String[] businessNo, Object bean) {
            Map<String, Map<String, Object>> functionMap = beanMap.get(clazz);
            if (functionMap == null) {
                functionMap = new HashMap<>();
                beanMap.put(clazz, functionMap);
            }
            Map<String, Object> businessMap = functionMap.get(function.getCode());
            if (businessMap == null) {
                businessMap = new HashMap<>();
                functionMap.put(function.getCode(), businessMap);
            }
            if (businessNo != null && businessNo.length > 0) {
                for (String no : businessNo) {
                    businessMap.put(no, bean);
                }
            }
        }

        public Object get(Class clazz, FunctionCodeEnum function, String businessNo) {
            Map<String, Object> businessMap = getBusinessMap(clazz, function);
            Object bean = businessMap.get(businessNo);
            if (bean == null) {
                String err = function.getCode() + ",businessNo:" + businessNo + " " + clazz.getSimpleName() + " mismatch";
                throw new IllegalArgumentException(err);
            }
            return bean;
        }

        private Map<String, Object> getBusinessMap(Class clazz, FunctionCodeEnum function) {
            Map<String, Map<String, Object>> functionMap = beanMap.get(clazz);
            if (functionMap == null) {
                String err = clazz.getSimpleName() + " mismatch";
                throw new IllegalArgumentException(err);
            }
            Map<String, Object> sellerMap = functionMap.get(function.getCode());
            if (sellerMap == null) {
                String err = function + " " + clazz.getSimpleName() + " mismatch";
                throw new IllegalArgumentException(err);
            }
            return sellerMap;
        }
    }
}
